import React, { Component } from 'react';
import {
	StyleSheet, Text, View, Image, Dimensions
} from "react-native";
import { style } from '../../theme/style';
import { Common } from '../../component/common/Common.js';
import HTMLView from 'react-native-htmlview';
import { GLOBAL } from '../../config/global';
import MessageService from './_common/service.js';
import { API, API_GENERATE } from '../../config/api';
import HTTP from '../../utils/http';
import StorageHelper from '../../utils/storageHelper';
import { STORAGE_CONSTANT } from '../../config/storage';

const scale = GLOBAL.SCALE;

export class MessageDetailsPage extends Component {

	static navigationOptions = ({ navigation }) => {
		let { title, navigatePress } = navigation.state.params;
		return {
			headerTitle: title + '详情',
			headerTintColor: style.color.white,
			headerLeft: <Common type="headLeft" onPress={() => { navigatePress && navigatePress() }} color='#fff' />,
			headerRight: (<Image style={{ marginLeft: 18 * scale, width: 9 * scale, height: 16 * scale }} source={require('../../imgs/goBackEmpty.png')} />),
			headerStyle: {
				elevation: 0,
				backgroundColor: style.color.themeColor,
			},
			headerTitleStyle: {
				flex: 1,
				textAlign: 'center'
			}
		}
	};

	constructor(props) {
		super(props);
		let { noticeNo, corpId } = this.props.navigation.state.params || {};
		this.state = {
			noticeNo: noticeNo,
			content: '',
			createTime: '',
			createBy: '',
			bizObjId: '',
			corpId: corpId
		}
		this.companyName = null;
	}

	_getUserInfo = async () => {
		if (!this.state.corpId) return;
		//获取用户信息
		this.userInfo = await StorageHelper.get(STORAGE_CONSTANT.USER_INFO_KEY) || {};
		if (this.userInfo) {
			this.userInfo = JSON.parse(this.userInfo);
		}
	}

	async	componentWillMount() {
		//已读 
		MessageService.readMessage({ noticeNo: this.state.noticeNo });

		await this._getUserInfo();
		//消息详情
		let detailData = await MessageService.detailsMessage({ noticeNo: this.state.noticeNo });
		if (detailData) {
			let { content, createTime, createBy, bizObjId, noticeSubject, status, redirect } = detailData;
			this.setState({
				noticeSubject,
				content,
				createTime,
				createBy,
				bizObjId,
				status,
				redirect  //授信账单 是否跳转  0:不跳转 1:跳转授信账单
			})
			if (redirect) {
				let responseData = await HTTP.POST(API_GENERATE(API.GET_CUSTOMER_DETAIL), {
					companyId: bizObjId,
					bizType: '01',
					corpId: this.state.corpId || this.userInfo.corpId
				});
				this.companyName = responseData.companyInfoVO.companyName || null;
			}
		}
	}

	componentDidMount() {
		this.props.navigation.setParams({
			navigatePress: this._navigatePress
		});
	}

	_navigatePress = () => {
		this.props.navigation.state.params.callBack();
		this.props.navigation.goBack();
	}

	render() {
		let { redirect, content, noticeSubject, createBy, createTime, bizObjId } = this.state;
		return (
			<View style={styles.container}>
				<View style={styles.box}>
					<View style={styles.titleBox}>
						<Text style={styles.title}>{noticeSubject}</Text>
						<View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }}>
							<View style={{ flexDirection: 'row', alignItems: 'center', justifyContent: 'flex-start' }}>
								<Text style={[styles.titleText]}>时间：{this.state.createTime && this.state.createTime.split(' ')[0]}</Text>
							</View>
						</View>
					</View>
					<View style={styles.body}>
						<HTMLView
							value={content}
						/>
						{redirect ? (<Text style={styles.text} onPress={() => { this.props.navigation.navigate('BillPage', { companyId: bizObjId, companyName: this.companyName }) }}>【查看明细】</Text>) : null}
					</View>
				</View>
			</View>
		)
	}
}

const styles = StyleSheet.create({
	container: {
		flex: 1,
		backgroundColor: '#FAFBFD'
	},
	box: {
		paddingLeft: 15 * scale,
		paddingRight: 15 * scale,
		paddingBottom: 25 * scale,
		backgroundColor: style.color.white,
		height: '100%'
	},
	titleBox: {
		borderBottomWidth: 1,
		borderStyle: 'solid',
		borderBottomColor: style.color.border,
	},
	title: {
		paddingTop: 13 * scale,
		fontSize: 16 * scale,
		color: style.color.normal
	},
	titleText: {
		fontSize: 12 * scale,
		color: '#8B8B8B',
		paddingBottom: 11 * scale,
		paddingTop: 6 * scale,
	},
	body: {
		paddingTop: 14 * scale
	},
	bodyText: {
		lineHeight: 18 * scale,
		fontSize: 13 * scale,
	},
	goPage: {
		color: style.color.themeColor,
		fontSize: 12 * scale,
	},
	text: {
		color: style.color.themeColor,
	}
})